//
//  LRXMyHelpItemCell.m
//  路人侠
//
//  Created by zhangxulong on 4/1/16.
//  Copyright © 2016 zhangxulong. All rights reserved.
//

#import "LRXMyHelpItemCell.h"
@interface LRXMyHelpItemCell()
@property (nonatomic,weak) LRXHelpMeInfoModel * model;

@property (nonatomic,strong) UILabel * thingGather;
@property (nonatomic,strong) UILabel * thingMoney;
@property (nonatomic,strong) UILabel * orderTime;

@property (nonatomic,strong) UILabel * createLabel;
@property (nonatomic,strong) UILabel * reLabel;
@property (nonatomic,strong) UIView * lineView;
@property (nonatomic,strong) UIView * lineView2;
@property (nonatomic,strong) UIButton * statusBtn;

@end

@implementation LRXMyHelpItemCell
- (UILabel *)createLabel
{
    if (_createLabel == nil) {
        _createLabel = [[UILabel alloc]init];
    }
    return _createLabel;
}

- (UILabel *)orderTime
{
    if (_orderTime == nil) {
        _orderTime = [[UILabel alloc]init];
    }
    return _orderTime;
}

- (UIImageView *)userView
{
    if (_userView == nil) {
        _userView = [[UIImageView alloc]init];
    }
    return _userView;
}
- (UILabel *)thingMoney
{
    if (_thingMoney == nil) {
        _thingMoney = [[UILabel alloc]init];
    }
    return _thingMoney;
}
- (UILabel *)thingGather
{
    if (_thingGather == nil) {
        _thingGather = [[UILabel alloc]init];
    }
    return _thingGather;
}
- (UILabel *)reLabel
{
    if (_reLabel == nil) {
        _reLabel = [[UILabel alloc]init];
    }
    return _reLabel;
}
- (UIView *)lineView
{
    if (_lineView == nil) {
        _lineView = [[UIView alloc]init];
    }
    return _lineView;
}
- (UIView *)lineView2
{
    if (_lineView2 == nil) {
        _lineView2 = [[UIView alloc]init];
    }
    return _lineView2;
}
- (UIButton *)statusBtn
{
    if (_statusBtn == nil) {
        _statusBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    }
    return _statusBtn;
}

- (void)layoutSubviews:(LRXHelpMeInfoModel *)model
{
  
    
    self.model = model;
    self.reLabel.text = @"下单时间";
    self.reLabel.font = [UIFont systemFontOfSize:20 * hScale];
    self.reLabel.frame = CGRectMake(32 * wScale, 22 * hScale, 0, 0);
    
    [self.reLabel sizeToFit];
    [self addSubview:self.reLabel];
    
    self.createLabel.text = model.createTime;
    self.createLabel.frame = CGRectMake(CGRectGetMaxX(self.reLabel.frame) + 24 * wScale, 22 * hScale, 0, 0);
    self.createLabel.font = [UIFont systemFontOfSize:20 * hScale];
    [self.createLabel sizeToFit];
    [self addSubview:self.createLabel];

    
    self.lineView.frame = CGRectMake(0, 57 * hScale, MainScreenW, 3 * hScale);
    self.lineView.backgroundColor = [UIColor colorWithRed:238 green:238 blue:238];
    [self addSubview:self.lineView];

    
    //用 SDWeBImage
    NSString * imageStr;
    if (model.userInfo.userImage   && ![model.userInfo.userImage isEqual:[NSNull null]] && model.userInfo.userImage.length > 0) {
        imageStr = [routeImageUrl stringByAppendingString:model.userInfo.userImage];
       
    }
    //转UTF8 编码
    imageStr = [imageStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL * imageUrl = [NSURL URLWithString: imageStr];
    [self.userView sd_setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"de"]];
    self.userView.frame = CGRectMake(32 * wScale, 71 * hScale, 118 * hScale, 118 * hScale);
    self.userView.layer.cornerRadius = self.userView.frame.size.height * 0.5;
    self.userView.layer.masksToBounds = YES;
    [self addSubview:self.userView];
    
    
    //设置物品具体显示标签
    self.thingGather.text = [NSString stringWithFormat:@"共: %ld件物品",model.thingArray.count];
    self.thingGather.font = [UIFont systemFontOfSize:24 * hScale];
    CGFloat sumMoney = 0;
    for (LRXThing * thing in model.thingArray)
    {
        sumMoney += [thing.thingMoney floatValue];
    }
    self.thingMoney.text = [NSString stringWithFormat:@"合计: ¥%.2f元 (含小费¥%@元)",sumMoney,model.tip];
    self.thingMoney.font = self.thingGather.font;
    
    CGFloat thingGatherX = CGRectGetMaxX(self.userView.frame) + 27 * wScale;
    CGFloat thingGatherY = CGRectGetMaxY(self.lineView.frame) + 28 * hScale;
    self.thingGather.frame = CGRectMake(thingGatherX, thingGatherY, 0, 0);
    [self.thingGather sizeToFit];
    
    CGFloat thingMoenyX = thingGatherX;
    CGFloat thingMoneyY = CGRectGetMaxY(self.thingGather.frame) + 23 * hScale;
    self.thingMoney.frame = CGRectMake(thingMoenyX, thingMoneyY, 0,0);
    [self.thingMoney sizeToFit];
    

    self.lineView2.frame = CGRectMake(0, 206 * hScale, MainScreenW, 3 * hScale);
    self.lineView2.backgroundColor = [UIColor colorWithRed:238 green:238 blue:238];
    [self addSubview:self.lineView2];
    
    self.statusBtn.backgroundColor = [UIColor colorWithRed:20 green:137 blue:241];
    
    
    
    [self.statusBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    self.statusBtn.titleLabel.font = [UIFont systemFontOfSize:27 * hScale];
    self.statusBtn.layer.cornerRadius = 5 * hScale;
    self.statusBtn.layer.masksToBounds = YES;
    self.statusBtn.frame = CGRectMake(400 * wScale, CGRectGetMaxY(self.lineView2.frame) + 13 * hScale, 189 * wScale, 56 * hScale);
    
    [self setStatusBtnText:model];
    [self addSubview:self.statusBtn];

    
    [self addSubview:self.thingGather];
    [self addSubview:self.thingMoney];
    [self addSubview:self.userView];
    
    
}
- (void)setStatusBtnText:(LRXHelpMeInfoModel *)model
{
    NSString * wilDoText = @"";
    UIColor * noEnableColor = [UIColor colorWithRed:255 green:156 blue:0];
    
    if (model.statue == 0) {
        wilDoText = @"再来一发";
        [self.statusBtn addTarget:self action:@selector(pushPayFunction) forControlEvents:UIControlEventTouchUpInside];
    }
    else if (model.statue == 1) {
        
        wilDoText = @"付款";
        [self.statusBtn addTarget:self action:@selector(pushPayFunction) forControlEvents:UIControlEventTouchUpInside];
    }
    else if(model.statue == 2)
    {
        wilDoText = @"等待接单";
        self.statusBtn.enabled = NO;
        [self.statusBtn setTitleColor:noEnableColor forState:UIControlStateNormal];
        self.statusBtn.backgroundColor = [UIColor whiteColor];
    }
    else if(model.statue == 3 )
    {
        if (model.orderModel.status == 1) {
            wilDoText = @"等待派送";
            self.statusBtn.enabled = NO;
            [self.statusBtn setTitleColor:noEnableColor forState:UIControlStateNormal];
            self.statusBtn.backgroundColor = [UIColor whiteColor];
        }
        else if(model.orderModel.status == 2)
        {
            wilDoText = @"确认收货";
            [self.statusBtn addTarget:self action:@selector(pushFinishOrder) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    else if(model.statue == 4)
    {
       if (model.orderModel.status == 3) {
            wilDoText = @"配送成功";
            self.statusBtn.enabled = NO;
            [self.statusBtn setTitleColor:noEnableColor forState:UIControlStateNormal];
            self.statusBtn.backgroundColor = [UIColor whiteColor];
        }
    }

    
    [self.statusBtn setTitle:wilDoText forState:UIControlStateNormal];

}

- (void)pushPayFunction
{
    
    if ([self.myHelpMoreInfoDelegate respondsToSelector:@selector(pushPayFunction:withRow:)]) {
        [self.myHelpMoreInfoDelegate pushPayFunction:self withRow:self.row];
    }
}
- (void)pushFinishOrder
{
    
    if ([self.myHelpMoreInfoDelegate respondsToSelector:@selector(pushFinishOrder:withRow:)]) {
        [self.myHelpMoreInfoDelegate pushFinishOrder:self withRow:self.row];
    }
}
//推出警告视图
- (void)pushReceviceAlerView
{
    if ([self.myHelpMoreInfoDelegate respondsToSelector:@selector(pushReceiveAlerView:withRow:)]) {
        [self.myHelpMoreInfoDelegate pushReceiveAlerView:self withRow:self.row];
    }
}



@end
